package com.intellagric.service.traceability.Impl.trace_produce_batch;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.ProduceBatchDao;
import com.intellagric.dao.traceability.ProduceDao;
import com.intellagric.mapper.TraceProduceBatchMapper;
import com.intellagric.module.traceability.ProduceBatchVo;
import com.intellagric.pojo.TraceProduceBatch;
import com.intellagric.pojo.TraceProduceBatchExample;
import com.intellagric.service.traceability.trace_produce_batch.ProduceBatchService;
import org.aspectj.weaver.tools.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/6
 * @DESC： 农产品生产批次管理服务实现
 */
@Service
public class ProduceBatchServiceImpl implements ProduceBatchService {

    @Autowired
    private TraceProduceBatchMapper produceBatchMapper;

    @Autowired
    private ProduceBatchDao produceBatchDao;
    /**
     * 添加批次信息
     * @param produceBatch
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addProduceBatch(TraceProduceBatch produceBatch) {
        if (produceBatchMapper.insertSelective(produceBatch) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除批次信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteProduceBatch(String id) {
        if (produceBatchMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改农产品批次信息
     * @param produceBatch
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editProduceBatch(TraceProduceBatch produceBatch) {
        if (produceBatchMapper.updateByPrimaryKeySelective(produceBatch) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询农产品批次信息
     * @param id
     * @return TraceProduceBatch
     */
    @Override
    public TraceProduceBatch getProduceBatchById(String id) {
        return produceBatchMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询农产品批次信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceBatchList() {
        TraceProduceBatchExample produceBatchExample = new TraceProduceBatchExample();
        produceBatchExample.createCriteria().andIdIsNotNull();
        List<TraceProduceBatch> traceProduceBatchList = produceBatchMapper.selectByExample(produceBatchExample);
        return LayuiDataGridResult.success().add(traceProduceBatchList,traceProduceBatchList.size());
    }
    @Override
    public LayuiDataGridResult getProduceBatchList(String createBy) {
        TraceProduceBatchExample produceBatchExample = new TraceProduceBatchExample();
        produceBatchExample.createCriteria().andCreateByEqualTo(createBy);
        List<TraceProduceBatch> traceProduceBatchList = produceBatchMapper.selectByExample(produceBatchExample);
        return LayuiDataGridResult.success().add(traceProduceBatchList,traceProduceBatchList.size());
    }

    /**
     * 分页查询农产品批次信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceBatchPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceProduceBatchExample produceBatchExample = new TraceProduceBatchExample();
        produceBatchExample.createCriteria().andIdIsNotNull();
        List<TraceProduceBatch> traceProduceBatchList = produceBatchMapper.selectByExample(produceBatchExample);
        PageInfo<TraceProduceBatch> pageInfo = new PageInfo<>(traceProduceBatchList);
        return LayuiDataGridResult.success().add(traceProduceBatchList,pageInfo.getTotal());
    }
    @Override
    public LayuiDataGridResult getProduceBatchPage(int page, int rows,String createBy) {
        PageHelper.startPage(page,rows);
        TraceProduceBatchExample produceBatchExample = new TraceProduceBatchExample();
        produceBatchExample.createCriteria().andCreateByEqualTo(createBy);
        List<TraceProduceBatch> traceProduceBatchList = produceBatchMapper.selectByExample(produceBatchExample);
        PageInfo<TraceProduceBatch> pageInfo = new PageInfo<>(traceProduceBatchList);
        return LayuiDataGridResult.success().add(traceProduceBatchList,pageInfo.getTotal());
    }

    /**
     * 通过VO类型查询农产品批次信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceBatchByVo(int page ,int rows,ProduceBatchVo produceBatchVo) {
        PageHelper.startPage(page,rows);
        List<TraceProduceBatch> traceProduceBatchList = produceBatchDao.selectProduceBatchByVo(produceBatchVo);
        PageInfo<TraceProduceBatch> pageInfo = new PageInfo<>(traceProduceBatchList);
        return LayuiDataGridResult.success().add(traceProduceBatchList,pageInfo.getTotal());
    }

    /**
     * 关联查询批次表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceBatchListWithRelation(int page ,int rows,ProduceBatchVo produceBatchVo) {
        PageHelper.startPage(page,rows);
        List<TraceProduceBatch> traceProduceBatchList = produceBatchDao.selectTraceList(produceBatchVo);
        PageInfo<TraceProduceBatch> pageInfo = new PageInfo<>(traceProduceBatchList);
        return LayuiDataGridResult.success().add(traceProduceBatchList,pageInfo.getTotal());
    }


}
